//
//  UICollectionViewHelper.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit

class UICollectionViewManagement: NSObject {
    var viewModel: UICollectionViewModelProtocol
    
    weak var cellDelegate: UICollectionViewCellDelegate?
    
    init(_ viewModel: UICollectionViewModelProtocol, cell delegate: UICollectionViewCellDelegate? = nil) {
        self.viewModel = viewModel
        cellDelegate = delegate
    }
}

extension UICollectionViewManagement: UICollectionViewDataSource {
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return viewModel.numberOfSections()
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let sectionModel = viewModel.sectionModel(at: section)
        return sectionModel.numberOfItems()
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let sectionModel = viewModel.sectionModel(at: indexPath.section)
        let data = sectionModel.dataForItem(at: indexPath.row)
        let cell = collectionView.dequeueReusableCell(withReuseIdentifierClass: data.dequeueReusableCell.self, for: indexPath)
        var cellSource = cell as? UICollectionViewCellDataSource
        cellSource?.delegate = cellDelegate
        //cellSource?.dataModel = data
        
        if let d = data as? BannerAdCellModel {
            print("-->toj: func:\(#function), number: \(d.number)")
        }
        
        return cell
    }
    
    //    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
    //
    //
    //        return view
    //    }
}

extension UICollectionViewManagement: UICollectionViewDelegate {
    
    func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        
        let sectionModel = viewModel.sectionModel(at: indexPath.section)
        let data = sectionModel.dataForItem(at: indexPath.row)
        var cellSource = cell as? UICollectionViewCellDataSource
        cellSource?.dataModel = data
        
//        guard let d = data as? BannerAdCellModel  else { return }
        //print("-->toj: func:\(#function), number: \(d.number), address:\(cell.cm_objectAddress)")
    }
    
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
//        let sectionModel = viewModel.sectionModel(at: indexPath.section)
//        let data = sectionModel.dataForItem(at: indexPath.row)
//        data.cellDidDisappear()
        
//        guard let d = data as? BannerAdCellModel  else { return }
       // print("-->toj: func:\(#function), number: \(d.number), address:\(cell.cm_objectAddress)")
        
        
//        collectionView.reloadItems(at: [indexPath])
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        cellDelegate?.collectionViewDidSelected(at: indexPath)
    }
}

// MARK: - 代理事件 UICollectionViewDelegateFlowLayout
extension UICollectionViewManagement: UICollectionViewDelegateFlowLayout {
    
    // cell 大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let sectionModel = viewModel.sectionModel(at: indexPath.section)
        let data = sectionModel.dataForItem(at: indexPath.row)
        
//        if let d = data as? BannerAdCellModel {
//            print("-->toj: func:\(#function), size:\(data.cellSize) number: \(d.number)")
//        }
        
        return data.cellSize
    }
    
    // section 上下左右的间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let sectionModel = viewModel.sectionModel(at: section)
        return sectionModel.layoutEdgeinset
    }
    
    // section header 大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        
        let sectionModel = viewModel.sectionModel(at: section)
        return sectionModel.headerSize
    }
    
    // section footer 大小
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        
        let sectionModel = viewModel.sectionModel(at: section)
        if section == collectionView.numberOfSections-1 {
            return .def_cipher
        }
        return sectionModel.footerSize
    }
    
    // cell之间 垂直间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        let sectionModel = viewModel.sectionModel(at: section)
        return sectionModel.minimumLineSpacing
    }
    
    // cell之间 水平间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        let sectionModel = viewModel.sectionModel(at: section)
        return sectionModel.minimumInteritemSpacing
    }
}
